<?php
	set_time_limit(0);
	include('commands.php'); // include the commands file
	init(); // starts the bot

	function init()
	{
		global $socket, $output, $core, $modules, $mods; // make the variables accessable by other functions

		$socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP); // create the socket
		$connection = socket_connect($socket,$core['server'],$core['port']); // connect
		if (!$connection) { print ("Could not connect to: ".$core['server']." on port ".$core['port']); } // if you don't connect you'll get this
		
		else
		{
			raw("USER ".$core['name']." ".$core['name']." ".$core['name']." :".$core['name'], "no");
			raw("NICK ".$core['nick'], "no");

			while ($output['all'] = trim(socket_read($socket,2046)))
			{
				if (strstr($output['all'], "001 ".$core['nick'])) //so it only does this once, and after it's fully connected
				{
					foreach ($core['channels'] as $channels) { raw("JOIN ". $channels); } // join all the channels from start.php
					raw("mode ".$core['nick']." +B", "no"); //marks itself as a bot to the network
					if ($core['nspass'] != null) { pm("NickServ", "IDENTIFY ". $core['nspass']); } //if a password is supplied it'll ident
				}
				buffer(); //makes the data all perrty
				process(); //executes the commands when people wanna execute them
			}
		}
	}

	function raw($command, $show = "yes")
	{
		global $socket, $output, $core;
		socket_write($socket, $command."\n\r");
		if ($show == "yes") { echo "Raw: ".$command."\n"; }
	}

	function msg($message)
	{
		global $socket, $output, $core;
		$command = "PRIVMSG ". $output['channel'] ." :".$message;
		socket_write($socket, $command."\n\r");
		echo "<".$core['nick']."> ".$message."\n";
	}
	
	function bdie($message)
	{
		global $socket, $output, $core;
		$command = "QUIT :".$message;
		socket_write($socket, $command."\n\r");
		echo "Quiting: ".$message."\n";
	}

	function pm($nick, $message)
	{
		global $socket, $output, $core;
		$command = "PRIVMSG ".$nick." :".$message;
		socket_write($socket, $command."\n\r");
		echo "PM: to <".$nick."> ".$message."\n";
	}

	function uptary($seconds,  $periods = null)
	{		
		if (!is_array($periods))
		{
			$periods = array (
				'years'	 => 31556926,
				'months'	=> 2629743,
				'weeks'	 => 604800,
				'days'	  => 86400,
				'hours'	 => 3600,
				'minutes'   => 60,
				'seconds'   => 1
				);
		}

		$seconds = (int) $seconds;
		foreach ($periods as $period => $value)
		{
			$count = floor($seconds / $value);

			if ($count == 0)
			{
				continue;
			}

			$values[$period] = $count;
			$seconds = $seconds % $value;
		}

		if (empty($values))
		{
			$values = null;
		}

		return $values;
	}

	function trim_array($x)
	{
		if (is_array($x))
		{
			return array_map('trim_array', $x);
		}
		return trim($x);
	}

	function admin($req = null)
	{
		global $output, $core;

		foreach ($core['admin'] as $owner => $details)
		{
			if (($owner == $output['username']) && ($details['host'] == $output['hostname']))
			{
				if ($details['lvl'] >= $req) { return true; }
			}
		}
		return false;
	}

	function buffer()
	{

		global $output, $core;
			
		$buffer = $output['all'];
		$buffer = explode(" ", $buffer, 4);

		$buffer['username'] = substr($buffer[0], 1, strpos($buffer['0'], "!")-1);


		$posExcl = strpos($buffer[0], "!");
		$posAt = strpos($buffer[0], "@");
		$buffer['identd'] = substr($buffer[0], $posExcl+1, $posAt-$posExcl-1); 
		$buffer['hostname'] = substr($buffer[0], strpos($buffer[0], "@")+1);
		$buffer['user_host'] = substr($buffer[0],1);
		$buffer['action'] = $buffer[1];
		if ($buffer[0] == "PING") { $buffer['action'] = "PING"; }
		
		switch (strtoupper($buffer['action']))
		{
			case "JOIN":
			   	$buffer['text'] = "*JOINS: ". $buffer['username']." ( ".$buffer['user_host']." )";
				$buffer['command'] = "JOIN";
				$buffer['channel'] = $core['channel'];
				echo "Joins: ".$buffer['username']."\n";
			   	break;

			case "QUIT":
			   	$buffer['text'] = "*QUITS: ". $buffer['username']." ( ".$buffer['user_host']." )";
				$buffer['command'] = "QUIT";
				$buffer['channel'] = $core['channel'];
				echo "Quit: ".$buffer['username']."\n";
			   	break;

			case "NOTICE":
				if (strstr($buffer[0], "**"))
				{
					echo "Notice: ".$buffer[0]." - Hostname found\n";
				}
				else
				{
					echo "Notice: ".$buffer['username']." - ".substr($buffer[3], 1)."\n";
				}
			   	$buffer['text'] = "*NOTICE: ". $buffer['username'];
				$buffer['command'] = "NOTICE";
				$buffer['channel'] = $buffer[2];

			   	break;

			case "PART":
			  	$buffer['text'] = "*PARTS: ". $buffer['username']." ( ".$buffer['user_host']." )";
				$buffer['command'] = "PART";
				$buffer['channel'] = $core['channel'];
				echo "Parts: ".$buffer['username']."\n";
			  	break;

			case "MODE":
			  	$buffer['text'] = $buffer['username']." sets mode: ".$buffer[3];
				$buffer['command'] = "MODE";
				$buffer['channel'] = $buffer[2];
				echo $buffer['text']."\n";
				break;

			case "NICK":
				$buffer['text'] = "*NICK: ".$buffer['username']." => ".substr($buffer[2], 1)." ( ".$buffer['user_host']." )";
				$buffer['command'] = "NICK";
				$buffer['channel'] = $core['channel'];
				echo $buffer['text']."\n";
				break;

			case "PING":
				raw('PONG :'.substr($output['all'], 6), "no");	
				break;

			case "PRIVMSG":
				$buffer['command'] = $buffer[1];
				$buffer['channel'] = $buffer[2];
				$buffer['text'] = substr($buffer[3], 1);
				echo "<".$buffer['username']."> ".$buffer['text']."\n";
				break;
		}
		$output = $buffer;
	}
?>
